package com.atguigu.serviceedu.controller;


import com.atguigu.serviceedu.entity.form.CourseInfoForm;
import com.atguigu.serviceedu.entity.vo.CoursePublishVO;
import com.atguigu.serviceedu.entity.vo.CourseQueryVO;
import com.atguigu.serviceedu.entity.vo.CourseVO;
import com.atguigu.serviceedu.service.EduCourseService;
import com.atguigu.vo.ResultVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2023-07-26
 */
@RestController
@RequestMapping("/serviceedu/course")
@CrossOrigin
public class EduCourseController {

    @Autowired
    private EduCourseService eduCourseService;
    @PostMapping("/addcourseinfo")
    public ResultVO addCourseInfo(@RequestBody CourseInfoForm courseInfoForm){
        String courseId = eduCourseService.insertCourseInfo(courseInfoForm);
        return ResultVO.ok().Msg("保存课程基本信息成功!").Data("courseId",courseId);
    }
    @GetMapping("/getCourseInfoById/{courseId}")
    public ResultVO getCourseInfoById(@PathVariable("courseId") String courseId){
        CourseInfoForm courseInfoForm = eduCourseService.selectCourseById(courseId);
        return ResultVO.ok().Msg("课程回显成功").Data("courseInfoForm",courseInfoForm);
    }
    @PutMapping("/editCourseInfo")
    public ResultVO editCourseInfo(@RequestBody CourseInfoForm courseInfoForm){
        boolean b = eduCourseService.updateCourseInfo(courseInfoForm);
        return ResultVO.ok().Msg("修改课程基本信息成功").Data("flag",b);
    }
    @PostMapping ("/listConditionCourse/{currentpage}/{pagesize}")
    public ResultVO listConditionCourse(@PathVariable Long currentpage, @PathVariable Long pagesize, @RequestBody CourseQueryVO courseQueryVO){
        IPage<CourseVO> pageModel = eduCourseService.getConditionCourse(currentpage, pagesize, courseQueryVO);
        return ResultVO.ok().Msg("课程列表分页成功").Data("total",pageModel.getTotal()).Data("itmes",pageModel.getRecords());
    }
    @DeleteMapping("/removeCourseById/{id}")
    public ResultVO removeCourseById(@PathVariable("id") String id){
        //boolean b = eduCourseService.deleteCourseCoverByCourseId(id);
        boolean b1 = eduCourseService.removeCourseById(id);
        return ResultVO.ok().Msg("删除课程成功").Data("flag",b1);
    }
    @GetMapping("/getCoursePublishById/{id}")
    public ResultVO getCoursePublishById(@PathVariable("id")String id){
        CoursePublishVO coursePublishVO = eduCourseService.queryCoursePublishById(id);
        return ResultVO.ok().Msg("请求课程发布信息成功").Data("items",coursePublishVO);
    }
    @PutMapping("/editCourseStatusById")
    public ResultVO editCourseStatusById(String id){
        boolean b = eduCourseService.updateCourseStatusById(id);
        if (b){
            return ResultVO.ok().Msg("课程发布成功").Data("flag",b);
        }else {
            return ResultVO.error().Msg("课程发布失败").Data("flag",b);
        }
    }
}

